﻿using System; 
using System.Text; 
using System.Security.Cryptography; 
using System.Collections.Generic; 
using System.Linq;
using suirui.ZhuMu.Common;
using suirui.ZhuMu.Framework;

namespace suirui.ZhuMu.Framework
{
    public class TestYunShiXunApi
    {
        private static string charset = "UTF-8";
        // 
        public static string sign(string KEY, Dictionary<string, string> parameters)
        {
            Dictionary<string, string> filteredParams = filterParameters(parameters);
            string strlinkString = linkParameters(filteredParams);
            Console.WriteLine(strlinkString);
            string result = MD5.sign(strlinkString, KEY, charset);
            return result;
        }

        public static Dictionary<string, string> filterParameters(Dictionary<string, string> parameters)
        {
            Dictionary<string, string> result = new Dictionary<string, string>(parameters) { };
            List<string> TempKey = new List<string>(result.Keys);
            for (int j = 0; j < result.Count;)
            {
                if (TempKey[j] == "sign" || TempKey[j] == null || TempKey[j] == "")
                {
                    result.Remove(TempKey[j]);
                    continue;
                }
                j++;
            }
            return result;
        }
        // 
        public static string linkParameters(Dictionary<string, string> parameters)
        {
            string result = "";
            // 
            Dictionary<string, string> Temp = parameters.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);

            int size = Temp.Count;
            int i = 0;
            foreach (KeyValuePair<string, string> x in Temp)
            {
                if (i == size - 1)
                {
                    result = result + x.Key + "=" + x.Value;
                }
                else
                {
                    result = result + x.Key + "=" + x.Value + "&";
                }
                i++;
            }
            return result;
        }
    }

    class MD5
    {
        public static string sign(string strText, string strKey, string charset)
        {
            if (strText == string.Empty || strKey == string.Empty)
                return string.Empty;

            strText += strKey;

            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            byte[] hashedDataBytes;
            hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding(charset).GetBytes(strText));
            StringBuilder tmp = new StringBuilder();
            foreach (byte i in hashedDataBytes)
            {
                tmp.Append(i.ToString("x2"));
            }
            return tmp.ToString();
        }
    }
}

// 
public class TestClass
{
    public static void Test()
    {
        // 
        string KEY = AppConfigration.Configuration["YunShiXun_key"];// "e10adc3949ba59abbe56e057f20f883e";
        // 
        string IDENTITY = AppConfigration.Configuration["YunShiXun_identity"];//"5b357915281c0fef984cd974bcd07f17";

        Dictionary<string, string> Mytest = new Dictionary<string, string> { };
        Mytest.Add("mobile", "13900001111");
        Mytest.Add("password", "123456");
        Mytest.Add("identity", IDENTITY);

        string strSign = TestYunShiXunApi.sign(KEY, Mytest);
        Console.WriteLine("sign:" + strSign);
        Console.Read();
        return;
    }
}
